package com.oss.view;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.oss.client.dto.OssSecurityResponse;
import org.oss.client.dto.UrlRequest;
import org.oss.client.dto.UrlResponse;
import org.oss.util.JsonMapper;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.oss.base.util.HttpUtil;
import com.oss.service.IStorageService;
import com.oss.service.IUserService;
import com.oss.vo.OssFileInfo;
import com.oss.vo.SysUser;

public class OssOperationAction extends ActionSupport{
	/** serialVersionUID*/
	private static final long serialVersionUID = -4658832797515511501L;

	Logger logger = Logger.getLogger(this.getClass());
	
	public String req;
	@Autowired
	IUserService userService;
	@Autowired
	private IStorageService storageService;
	
	
	@Action("getUrl")
	public void getUrl(){
		try {
			UrlRequest reqObj = JsonMapper.buildNonDefaultMapper().fromJson(req, UrlRequest.class);
			OssFileInfo file = storageService.getFileById(reqObj);
			SysUser user = userService.getUserByLoginName(reqObj.getUser());
			OssSecurityResponse res = userService.getUserSecurity(user, reqObj);
			UrlResponse resUrl = storageService.genFileUrl(reqObj,res,file);
			
			HttpUtil.writeRes(resUrl);
		} catch (Exception e) {
			e.printStackTrace();
			HttpUtil.writeRes(HttpUtil.buildFailedRes(UrlResponse.class, e));
		}
	}
	
	
	
	
	

	public String getReq() {
		return req;
	}

	public void setReq(String req) {
		this.req = req;
	}
	
	
	
	
	
	

}
